/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.tests.persistance;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import ar.com.wholeconsulting.pacc.model.Acero;

/**
 * 
 * @author pable
 */
public class AcerosParcer {

	public static Acero parseLine(String line) {
		Acero acero = new Acero();
		line = line.replaceAll(",", ".");
		line = line.replaceAll("\"", "");
		String[] vec = line.split(";");
		acero.setNombre(vec[0]);
		Double[] xs = { 150.0, 200.0, 250.0, 300.0, 400.0, 500.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0 };
		List<Double> lis = Arrays.asList(xs);
		acero.getxValues().addAll(lis);
		for (int i = 1; i < vec.length; i++) {
			acero.getyValues().add(Double.parseDouble(vec[i]));
		}
		return acero;
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		// JPAUtil util = new JPAUtil();
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("PACC");
		EntityManager manager = factory.createEntityManager();
		EntityTransaction transaction = manager.getTransaction();
		transaction.begin();

		Query query = manager.createNamedQuery("Acero.findAll");
		List<Acero> aceros = query.getResultList();
		for (Acero a : aceros) {
			System.out.println(a.getNombre());
			System.out.println(a.getxValues());
			System.out.println(a.getyValues());
			System.out.println("----------------------------------------");
		}

//		try {
//			// Open the file that is the first
//			// command line parameter
//			FileInputStream fstream = new FileInputStream("C:/Users/Duco Pablo/Documents/NetBeansProjects/PACC/src/main/resources/base/ASTM.csv");
//			// Get the object of DataInputStream
//			DataInputStream in = new DataInputStream(fstream);
//			BufferedReader br = new BufferedReader(new InputStreamReader(in));
//			String strLine;
//			// Read File Line By Line
//			while ((strLine = br.readLine()) != null) {
//				// Print the content on the console
//				manager.persist(parseLine(strLine));
//			}
//			// Close the input stream
//			in.close();
//		} catch (Exception e) {// Catch exception if any
//			System.err.println("Error: " + e.getMessage());
//		}
//
//		transaction.commit();

		manager.close();
		factory.close();
	}
}
